﻿using Reticulum.Communication;
using Reticulum.Properties;
using Reticulum.User;
using Reticulum.工具类;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Reticulum
{
    public partial class LoginHome : Form
    {
        public LoginHome()
        {
            InitializeComponent();
        }
        
        //-----------------------------------------------------------------------
        public static Communication.MasterForm masterForm = new Communication.MasterForm();
        private void Form1_Load(object sender, EventArgs e)
        {
            //SMS.AliyunSMS();

            //创建链接会话
            masterForm.Show();
            masterForm.Hide();


            //设置窗体标题
            Text = "Chages云盘";
            //链接数据库
            DBHelps.Connection();
            string select = string.Format("select * from Users");
            while (DBHelps.Select(select).Read())
            {

                if (DBHelps.Select(select)["RememberPwd"].ToString() == "1")
                {
                    LoginText.Text = DBHelps.Select(select)["UserAccount"].ToString();
                    LoginPrssword.Text= DBHelps.Select(select)["UserPwd"].ToString();
                    Remember.Checked = true;
                }
            }
            DBHelps.Select(select).Close();
            DBHelps.reader = null;
        }

        //-----------------------------------------------------------------------
        private void label1_Click(object sender, EventArgs e)
        {
            //实例化winfrom窗体
            Zhuche zhuche = new Zhuche();

            //调用Show方法，显示模式窗体
            zhuche.ShowDialog();
        }


        //结束程序的按钮---------------------------------------------------------------------
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            //Environment.Exit(0); 结束程序的方法
            Application.Exit();   //结束窗体的方法
        }

        //无边框拖动窗体-----------------------------------------------------------------------------------
        [DllImport("user32.dll")]//*********************拖动无窗体的控件  需要引用
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;
        
        private void LoginHome_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);//*********************调用移动无窗体控件函数
        }
        private void LoginHome_MouseMove(object sender, MouseEventArgs e)
        {
            
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void axWindowsMediaPlayer1_Enter(object sender, EventArgs e)
        {

        }



        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void tis2_Click(object sender, EventArgs e)
        {
            //tis2.BackgroundImageLayout = 
        }
        //----------------------------------------------------------------------------------------
        public static string account;//存在现在在操作的用户账号
        public static string pwd;//存在现在在操作的用户密码
        public static string ThisIP;//存在现在在操作的用户密码
        public static string Seal;//存在现在在操作的用户是否被封禁

        private void pictureBox3_MouseDown(object sender, MouseEventArgs e)
        {
            
        }//------------------------------------------------------------------------------------------

        private void OpenEyes_MouseDown(object sender, MouseEventArgs e)
        {
            OpenEyes.BackgroundImage = Resources.闭眼;
            this.LoginPrssword.PasswordChar = '\0';
            //LoginPrssword.Text = 
        }

        private void OpenEyes_MouseUp(object sender, MouseEventArgs e)
        {
            OpenEyes.BackgroundImage = Resources.睁眼;
            this.LoginPrssword.PasswordChar = '*';
        }//-------------------------------------------------------------------

        private void OpenEyes_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click_1(object sender, EventArgs e)
        {
            ForgetPWD forgetPWD = new ForgetPWD();
            forgetPWD.Show();
        }

        private void label2_Click_1(object sender, EventArgs e)
        {

        }

        //-----------------------------------------------管理员  和   用户的切换
        public static bool mastergo = true;
        private void master_Click(object sender, EventArgs e)
        {
            if (mastergo)
            {
                //管理员模式
                MessageBox.Show("切换至Administrator登录模式！", "", MessageBoxButtons.OK);
                master.Text = "用户模式";
                master.ForeColor = Color.White;//更改字体颜色

                //切换背景
                label2.Image = Resources.登录背景动画;
                this.BackgroundImage = Resources.管理员模式;
                //隐藏相关控件
                Remember.Visible = false ;
                label1.Visible = false;
                Backprssword.Visible = false;
                zhuche.Visible = false;
            }
            else
            {
                //用户模式
                master.Text = "管理员模式";
                master.ForeColor = Color.Black;//更改字体颜色
                label2.Image = Resources.登录背景动画2;
                this.BackgroundImage = Resources.Login;
                Remember.Visible = true;
                label1.Visible = true;
                Backprssword.Visible = true;
                zhuche.Visible = true;
            }
            mastergo = !mastergo;
        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            if (LoginText.Text != "" && LoginPrssword.Text != "")
            {
                //判断管理员模式
                if (master.Text == "用户模式" && LoginText.Text == "1907" && LoginPrssword.Text == "123")
                {
                    MessageBox.Show("进入管理员模式", "", MessageBoxButtons.OK);
                    //管理员窗口
                    Hide();
                    Master master = new Master();
                    master.ShowDialog();
                    this.Close();
                }
                else if (master.Text == "用户模式")
                {
                    MessageBox.Show("管理员密码错误", "", MessageBoxButtons.OK);
                }
                else if (master.Text == "管理员模式")//判断用户模式
                {
                    //将text输入的数据与SQL的数据库进行匹配
                    string sql2 = string.Format("select UserAccount,UserPwd,ThisIP,Seal from Users where UserAccount='{0}' and UserPwd='{1}'",ReplaceSQLChar.ReplaceSQL(LoginText.Text), ReplaceSQLChar.ReplaceSQL(LoginPrssword.Text));//Seal为0就没被封号

                    //初始化 正在输入的账号和密码
                    account = "";
                    pwd = "";
                    ThisIP = "";
                    Seal = "";

                    SqlDataReader reader = DBHelps.Select(sql2);
                    //调用DBHelps类的Select()方法 对 数据进行赋值
                    while (reader.Read())
                    {
                        account = reader["UserAccount"].ToString();
                        pwd = reader["UserPwd"].ToString();
                        ThisIP = reader["ThisIP"].ToString();
                        Seal = reader["Seal"].ToString();
                    }
                    //关闭reader对象
                    DBHelps.Select(sql2).Close();
                    DBHelps.reader = null;

                    if (Seal=="0")
                    {
                        if (account != "" && pwd != "" && ThisIP == "")//登录成功
                        {
                            //录入用户信息
                            ThisMyInfo.ThisUser();
                            string Use = string.Format("update Users set ThisIP = '{0}' where UserID='{1}'", ThisMyInfo.ThisIP, ThisMyInfo.UserID);

                            DBHelps.Command(Use);


                            //切换客户端窗口
                            //MessageBox.Show("登录成功", "提示", MessageBoxButtons.OK);

                            if (Remember.Checked)//判断是否选中记住密码复选框
                            {//选中后RememberPwd列为1记住密码
                                string rem = string.Format("update Users set RememberPwd = 0");//RememberPwd
                                DBHelps.Command(rem);
                                string re = string.Format("update Users set RememberPwd = 1 where UserAccount='{0}' and UserPwd='{1}' ", account, pwd);
                                DBHelps.Command(re);
                            }
                            else
                            {
                                //没有选中的状态下RememberPwd列为0
                                string rem = string.Format("update Users set RememberPwd = 0");//RememberPwd
                                DBHelps.Command(rem);
                            }
                            this.Hide();
                            Loads load = new Loads();
                            load.ShowDialog();
                            this.Close();
                            /*
                            this.Hide();
                            Client client = new Client();
                            client.ShowDialog();
                            this.Close();
                            */
                        }
                        else if (account != "" && pwd != "" && ThisIP != "")
                        {
                            tis.Text = "有人正在登录这个账号";
                            tis.ForeColor = Color.Red;
                            tis2.BackgroundImageLayout = ImageLayout.Zoom;
                        }
                    }
                    else if (Seal=="1")
                    {
                        MessageBox.Show("你的账号已被封禁了！\n请联系管理员！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        tis.Text = "账号或密码错误！请重新输入";
                        tis.ForeColor = Color.Red;
                        tis2.BackgroundImageLayout = ImageLayout.Zoom;
                    }
                }
            }
            else if (LoginText.Text == "" || LoginPrssword.Text == "")
            {
                tis.Text = "请输入用户名或密码";
                tis.ForeColor = Color.Red;
                tis2.BackgroundImageLayout = ImageLayout.Zoom;
            }
        }

        private void LoginText_KeyPress(object sender, KeyPressEventArgs e)
        {
            //在输入用户名框中键盘回车可以进行登陆
            if (e.KeyChar == (char)13)//ASCII 13为回车的码
            {
                pictureBox3_Click(sender, e);//使用登陆按钮方法
            }
        }

        private void LoginPrssword_KeyPress(object sender, KeyPressEventArgs e)
        {
            //在输入密码框中键盘回车可以进行登陆
            if (e.KeyChar == (char)13)//ASCII 13为回车的码
            {
                pictureBox3_Click(sender, e);//使用登陆按钮方法
            }
        }

        private void LoginText_TextChanged(object sender, EventArgs e)
        {
            //对text输入数据时，隐藏报错提示
            tis2.BackgroundImageLayout = ImageLayout.None;
            tis.Text = "";

            if (LoginText.Text == "")
            {
                label3.Visible = true;
            }
            else
            {
                label3.Visible = false;
            }
            Remember.Checked = false;
        }

        private void LoginPrssword_TextChanged(object sender, EventArgs e)
        {
            //对text输入数据时，隐藏报错提示
            tis2.BackgroundImageLayout = ImageLayout.None;
            tis.Text = "";
            if (LoginPrssword.Text == "")
            {
                label4.Visible = true;
            }
            else
            {
                label4.Visible = false;
            }
        }
    }
}
